package com.xiaozhen.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaozhen.common.core.domain.system.SysUser;
import com.xiaozhen.common.core.utils.StringUtils;
import com.xiaozhen.system.dao.SysDeptDao;
import com.xiaozhen.common.core.domain.system.SysDept;
import com.xiaozhen.common.core.domain.vo.TreeSelect;
import com.xiaozhen.system.dao.SysUserDao;
import com.xiaozhen.system.service.SysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * HISTORY
 *
 * @author zhenweisheng
 * @date 2021/8/19 14:21
 * @description:
 */
@Service
public class SysDeptServiceImpl implements SysDeptService {
    @Autowired
    private SysDeptDao sysDeptDao;
    @Autowired
    private SysUserDao sysUserDao;

    @Override
    public List<SysDept> selectDeptList(SysDept dept) {
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag", 0);
        if (dept.getParentId() != null && dept.getParentId() != 0) {
            queryWrapper.eq("parent_id", dept.getParentId());
        }
        if (dept.getStatus() != null) {
            queryWrapper.eq("status", dept.getStatus());
        }
        if (StringUtils.isNotEmpty(dept.getDeptName())) {
            queryWrapper.like("dept_name", dept.getDeptName());
        }
        return sysDeptDao.selectList(queryWrapper);
    }

    @Override
    public List<TreeSelect> buildDeptTreeSelect(List<SysDept> depts) {
        List<SysDept> deptTrees = buildDeptTree(depts);
        return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }


    /**
     * 构建前端所需要树结构
     *
     * @param depts 部门列表
     * @return 树结构列表
     */
    @Override
    public List<SysDept> buildDeptTree(List<SysDept> depts) {
        List<SysDept> returnList = new ArrayList<>();
        List<Long> tempList = new ArrayList<>();
        for (SysDept dept : depts) {
            tempList.add(dept.getDeptId());
        }
        for (Iterator<SysDept> iterator = depts.iterator(); iterator.hasNext(); ) {
            SysDept dept = iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(dept.getParentId())) {
                recursionFn(depts, dept);
                returnList.add(dept);
            }
        }
        if (returnList.isEmpty()) {
            returnList = depts;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<SysDept> list, SysDept t) {
        // 得到子节点列表
        List<SysDept> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysDept tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<SysDept> getChildList(List<SysDept> list, SysDept t) {
        List<SysDept> tlist = new ArrayList<SysDept>();
        Iterator<SysDept> it = list.iterator();
        while (it.hasNext()) {
            SysDept n = it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getDeptId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysDept> list, SysDept t) {
        return getChildList(list, t).size() > 0 ? true : false;
    }

    @Override
    public Boolean checkDeptNameUnique(SysDept dept) {
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("dept_id");
        queryWrapper.eq("dept_name", dept.getDeptName());
        List<SysDept> sysDepts = sysDeptDao.selectList(queryWrapper);
        if (sysDepts.size() == 0) {
            return true;
        } else if (dept.getDeptId() != null && dept.getDeptId().equals(sysDepts.get(0).getDeptId())) {
            return true;
        }
        return false;
    }

    @Override
    public Integer insertSysDept(SysDept dept) {
        return sysDeptDao.insert(dept);
    }

    @Override
    public Integer updateSysDept(SysDept dept) {
        return sysDeptDao.updateById(dept);
    }

    @Override
    public Boolean hasChildByDeptId(Long deptId) {
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", deptId);
        Integer count = sysDeptDao.selectCount(queryWrapper);
        return count > 0 ? true : false;
    }

    @Override
    public Boolean checkDeptExistUser(Long deptId) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dept_id", deptId);
        Integer count = sysUserDao.selectCount(queryWrapper);
        return count > 0 ? true : false;
    }

    @Override
    public Integer deleteDeptById(Long deptId) {
        return sysDeptDao.deleteById(deptId);
    }
}
